;*****************************************************************************************************
;+
; NAME:
;       IGL_COORD
;
; PURPOSE:
;
;       This is a subclass of a CATCOORD object which represents a
;       coordinate system. It adds flags to know if the data ranges
;       are logarithmic and to control if they can be modified or not
;       (static coordinate system). It also automatically refreshes
;       the control panel of its parents whenever the data ranges are modified.
;
;
; AUTHORS:
;
;        RAPHAEL SADOUN
;        Graduate Student
;        Institut d'Astrophysique de Paris
;        98 bis boulevard Arago
;        75014 PARIS FRANCE
;        E-mail: raphael.sadoun@gmail.com
;
; SUPERCLASSES:
;
;       CATCOORD (Catalyst Library)
;
; SYNTAX:
;
;       coord = Obj_New('IGL_Coord')
;
;
; CLASS_STRUCTURE:
;
;          class = { IGL_Coord, $
;             INHERITS CATCOORD, $ ; This is a CATCOORD object
;             xlog:0B, $           ; Flag indicating if the x data range is logarithmic
;             ylog:0B, $           ; Flag indicating if the y data range is logarithmic
;             static:0B $          ; Flag indicating if the data ranges are static (cannot be modified)
;           }
;
;
; MODIFICATION_HISTORY:
;
;       Written by: Raphael Sadoun, 24 April 2012
;-
;*****************************************************************************************************
;+
; NAME:
;       IGL_Coord::DRAW
;
; PURPOSE:
;
;       This method apply the coordinate system.
;
; SYNTAX:
;
;       self -> Draw
;
; ARGUMENTS:
;
;       None.
;
; KEYWORDS:
;
;       None.
;
;-
;*****************************************************************************************************
PRO IGL_Coord::Draw, Extra=extrakeywords

   @cat_pro_error_handler
   
   ;; You cannot do the next part successfully, unless there is an open window.
   IF ((!D.FLAGS AND 256) NE 0) AND !D.Window LT 0 THEN BEGIN
        createdWindow = 1
        Window, /Pixmap
   ENDIF ELSE createdWindow = 0
   
   ;; Draw an invisible plot
   Plot, self._xrange, self._yrange, POSITION=self._position, $
         XSTYLE=5, YSTYLE=5, /NODATA, /NOERASE,  $
         XRANGE=self._xrange, YRANGE=self._yrange, $
         XLOG=self.xlog, YLOG=self.ylog
   
   ;; Clean up. 
   IF createdWindow THEN WDelete,  !D.Window

   ;; Report completion
   self -> Report, /Completed

END

;*****************************************************************************************************
;+
; NAME:
;       IGL_Coord::GETPROPERTY
;
; PURPOSE:
;
;       This method is used to get the object's properties.
;
; SYNTAX:
;
;       self -> GetProperty
;
; ARGUMENTS:
;
;     None.
;
; KEYWORDS:
;
;     XLOG:           The flag indicating if the x data range is logarithmic.
;
;     YLOG:           The flag indicating if the y data range is logarithmic.
;
;     STATIC:        The flag indicating if the coordinate system
;                            is static (i.e. can not be modified). 
;
;     _REF_EXTRA:     Any keywords appropriate for the superclass GetProperty method.
;-
;*****************************************************************************************************
PRO IGL_Coord::GetProperty, XLOG=xlog, $
                            YLOG=ylog, $
                            STATIC=static, $
                            _REF_EXTRA=extraKeywords

   @cat_pro_error_handler

   ; Object properties.
   IF (arg_present(xlog)) THEN xlog = self.xlog
   IF (arg_present(ylog)) THEN ylog = self.ylog 
   IF (arg_present(static)) THEN static = self.static

   ; Need superclass properties?
   self -> CATCOORD::GetProperty, _Extra=extraKeywords

   self -> Report, /Completed

END

;*****************************************************************************************************
;+
; NAME:
;       IGL_Coord::SETPROPERTY
;
; PURPOSE:
;
;       This method is used to set the object's properties.
;
; SYNTAX:
;
;       self -> SetProperty
;
; ARGUMENTS:
;
;     None.
;
; KEYWORDS:
;
;     XLOG:           The flag indicating if the x data range is logarithmic.
;
;     YLOG:           The flag indicating if the y data range is logarithmic.
;
;     STATIC:        The flag indicating if the coordinate system
;                            is static (i.e. can not be modified). 
;
;     _EXTRA:       Any keyword accepted by the SetProperty method of
;                   the CATCOORD superclass.
;-
;*****************************************************************************************************
PRO IGL_Coord::SetProperty, XRANGE=xrange, $
                            YRANGE=yrange, $
                            XLOG=xlog, $
                            YLOG=ylog, $
                            STATIC=static, $
                            _EXTRA=extraKeywords

   @cat_pro_error_handler

   needUpdate = 0 ; Flag to know if we need to refresh the control panels of the parents

   ; Object properties.
   IF (n_elements(xlog) NE 0) THEN self.xlog = keyword_set(xlog)
   IF (n_elements(ylog) NE 0) THEN self.ylog = keyword_set(ylog)
   IF (n_elements(static) NE 0) THEN self.static = keyword_set(static)

   ;; Change the x data range
   IF (n_elements(xrange) NE 0) THEN BEGIN

      IF xrange[0] NE xrange[1] THEN BEGIN ; Make sure we have a valid range
         
         IF self.static EQ 0 THEN BEGIN ; Can the data range be modified ?
            
            IF (self.xlog) THEN BEGIN ; If it is logarithmic, only allow positive values
               
               xmin = min(xrange)
               IF (xmin GT 0) THEN BEGIN
                  self._xrange = xrange
                  needUpdate = 1
               ENDIF
               
            ENDIF ELSE BEGIN
               
               self._xrange = xrange
               needUpdate = 1
               
            ENDELSE
            
         ENDIF
      ENDIF
   ENDIF 

    ;; Change the y data range
   IF (n_elements(yrange) NE 0) THEN BEGIN
      
      IF yrange[0] NE yrange[1] THEN BEGIN ; Make sure we have a valid range
         
         IF self.static EQ 0 THEN BEGIN ; Can the data range be modified ?
            
            IF (self.ylog) THEN BEGIN ; If it is logarithmic, only allow positive values
               
               ymin = min(yrange)
               IF (ymin GT 0) THEN BEGIN
                  self._yrange = yrange
                  needUpdate = 1
               ENDIF
               
            ENDIF ELSE BEGIN
               
               self._yrange = yrange
               needUpdate = 1
               
            ENDELSE
            
         ENDIF
      ENDIF
   ENDIF
   
   ; Need superclass properties?
   self -> CATCOORD::SetProperty, _Extra=extraKeywords

   ;; Refresh the control panels of all possible CatDataAtom parents 
   IF needUpdate THEN BEGIN
      self -> GetProperty, PARENTS=parents
      validParents = where(OBJ_ISA_VALID(parents,'CatDataAtom'),count)
      FOR i=0, count-1 DO BEGIN
         parents[validParents[i]] -> GetProperty, CONTROLPANEL=cp
         IF OBJ_VALID(cp) THEN cp -> Refresh_Properties
      ENDFOR
   ENDIF

   self -> Report, /Completed

END

;*****************************************************************************************************
;+
; NAME:
;       IGL_Coord::CLEANUP
;
; PURPOSE:
;
;       This is the IGL_Coord object class destructor method.
;
; SYNTAX:
;
;       Called automatically when the object is destroyed.
;
;
; ARGUMENTS:
;
;     None.
;
; KEYWORDS:
;
;     None.
;-
;*****************************************************************************************************
PRO IGL_Coord::CLEANUP

   @cat_pro_error_handler

   self -> CATCOORD::CLEANUP

   self -> Report, /Completed

END

;*****************************************************************************************************
;+
; NAME:
;       IGL_Coord::INIT
;
; PURPOSE:
;
;       This is the IGL_Coord object class creator method.
;
; SYNTAX:
;
;       Called automatically when the object is created.
;
; ARGUMENTS:
;
;     None.
;
; KEYWORDS:
;
;     XLOG:           The flag indicating if the x data range is logarithmic.
;
;     YLOG:           The flag indicating if the y data range is logarithmic.
;
;     STATIC:        The flag indicating if the coordinate system
;                            is static (i.e. can not be modified). 
;
;     _EXTRA:       Any keywords accepted by the CATCOORD::Init method.
;
;-
;*****************************************************************************************************
FUNCTION IGL_Coord::Init, STATIC=static, $
                          XLOG=xlog, $
                          YLOG=ylog, $
                          _REF_EXTRA=extraKeywords

   @cat_func_error_handler
   
   ;; Call the superclass INIT method.
   ok = self->CATCOORD::INIT(_Extra=extraKeywords)
   IF NOT ok THEN RETURN, 0

   ;; Load the object.
   self.xlog = keyword_set(xlog)
   self.ylog = keyword_set(ylog)
   self.static = keyword_set(static)

   ;; Report completion
   self -> Report, /Completed
   RETURN, 1

END

;*****************************************************************************************************
;
; NAME:
;       IGL_COORD CLASS DEFINITION
;
; PURPOSE:
;
;       This is the IGL_Coord object's structure definition code.
;
;*****************************************************************************************************
PRO IGL_Coord__define, class

  COMPILE_OPT idl2, HIDDEN

   class = { IGL_Coord, $
             INHERITS CATCOORD, $ ; This is a CATCOORD object
             xlog:0B, $           ; Flag indicating if the x data range is logarithmic
             ylog:0B, $           ; Flag indicating if the y data range is logarithmic
             static:0B $          ; Flag indicating if the data ranges are static (cannot be modified)
           }

   struct_hide, class

END
